# 管道 数据的处理 待定接口的类
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
# 1、导包
import pymysql
from itemadapter import ItemAdapter
from scrapy.utils.project import get_project_settings

# 2、创建连接对象
class DcPipeline:
    # 初始化数据处理
    def __init__(self):
        settings = get_project_settings()
        self.host = settings['HOST']
        self.port = settings['PORT']
        self.user = settings['USER']
        self.password = settings['PASSWD']
        self.db = settings['DB']
        self.character = settings['CHARACTER']
        self.connect()

    def connect(self):
        self.conn = pymysql.connect(
            host=self.host,
            port=self.port,
            user=self.user,
            password=self.password,
            database=self.db,
            charset=self.character
        )
        # 3、创建游标
        self.cursor = self.conn.cursor()

    def process_item(self, item, spider):
        sql = 'insert into words(english,chinese,first_word,del_flag) values("{}","{}","{}","{}")'.format(item['word'],item['chinese'],item['firstWord'],item['del_flag'])
        # 4、执行sql语句
        self.cursor.execute(sql)
        # 5、提交事务
        self.conn.commit()

        return item

    def __del__(self):
        # 6、关闭游标
        self.cursor.close()
        # 7、关闭连接
        self.conn.close()
